<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of commentaires_controller
 *
 * @author morientez
 */
class CommentairesController extends AppController {

    var $name = 'Commentaires';
    var $helpers = array('Html', 'Javascript');
    var $components = array('Auth');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->userModel = 'admin';
        $this->Auth->fields = array('username' => 'user', 'password' => 'mot-passe');
        $this->Auth->allow('ajouterCommentaire');
        $this->Auth->loginAction = array('controller' => 'admins', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
        $this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
    }

    function ajouterCommentaire() {

        $info = $this->data;
        $tipo = $info['Commentaire']['typefi'];
        $today = date('Y-m-d H:i:s');
        $info['Commentaire']['date'] = $today;

        unset($info['Commentaire']['typefi']);
        if ($this->Commentaire->save($info)) {
            if ($tipo == 'video') {
                $this->redirect(array('controller' => 'fichiers', 'action' => 'galerie_videos'));
            } else if ($tipo == 'audio') {
                $this->redirect(array('controller' => 'fichiers', 'action' => 'galerie_audios'));
            } else {
                $this->redirect(array('controller' => 'fichiers', 'action' => 'galerie_jeux'));
            }
        } else {
            if ($tipo == 'video') {
                $this->redirect(array('controller' => 'fichiers', 'action' => 'video', $info['Commentaire']['fichier_id'], 0));
            } else if ($tipo == 'audio') {
                $this->redirect(array('controller' => 'fichiers', 'action' => 'audio', $info['Commentaire']['fichier_id']));
            } else {
                $this->redirect(array('controller' => 'fichiers', 'action' => 'jeux', $info['Commentaire']['fichier_id']));
            }
        }
    }

    function valider_liste() {

        if (empty($this->data)) {
            $this->loadModel('Fichier');

            $this->set('fichiers', $this->Fichier->find('all'));
            $this->loadModel('Commentaire');
        }

        /* if (empty($this->data)) {

          $this->set('commentaires', $this->Commentaire->find('all'));

          } */
    }

    function edit_effacer() {

        $form = $this->data;

        $etat = $form['Commentaire']['etat'];
        $id = $form['Commentaire']['id'];

        if ($etat == 1) {
            $resultat = $this->Commentaire->save($form);

            if ($resultat) {
                echo "<script> alert('Comentaire Enregistré');</script>";

                echo "<script lenguage='javascript'>";
                echo "location='valider_liste'";
                echo "</script>";
            }
        } else {


            $resultat = $this->Commentaire->delete($id);
            if ($resultat) {
                echo "<script> alert('Commentaire Effacé');</script>";

                echo "<script lenguage='javascript'>";
                echo "location='valider_liste'";
                echo "</script>";
            }
        }
    }

    function liste_editer_coments() {

        $this->set('commentaires', $this->Commentaire->find('all'));
    }

    function modifier_coments_final($id_coment) {

        $this->Commentaire->id = $id_coment;
        $this->Commentaire->id;
        $this->set('commentaires', $this->Commentaire->read());
    }

    function modifier_complete_coments() {


        $form = $this->data;
        $ima = $this->data;



	if (empty($this->data)==false) {
		//print_r ($this->data);

		//$this->Fichier->id = $id_art;
		$resultat = $this->Commentaire->save($form);

	if ($resultat) {
			 echo "<script> alert('Commentaire Modifie');</script>";

                echo "<script lenguage='javascript'>";
                echo "location='liste_editer_coments'";
                echo "</script>";
		}
            }

    }

         function efaccer_coments_final($id){

             if($this->Commentaire->delete()){
            $this->Session->setFlash('Le commentaire a été effacé .');
            $this->redirect(array('action' => 'liste_editer_coments'));
        }


 }

}
?>
